//
//  LinkageSlidingTableView.m
//  project-demo-oc
//
//  Created by 赵隆杰 on 2021/5/31.
//

#import "LinkageSlidingTableView.h"

@interface LinkageSlidingTableView () <UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, assign) NSInteger index;

@property (nonatomic, assign) BOOL tableCanScroll;
@property (nonatomic, strong) NSMutableArray *dataSources;

@end

@implementation LinkageSlidingTableView

- (instancetype)initWithIndex:(NSInteger)index
{
    self = [super init];
    if (self) {
        self.index = index;
        self.backgroundColor = [UIColor lightGrayColor];
        self.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
        self.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
        self.estimatedRowHeight = 50;
        self.dataSource = self;
        self.delegate = self;
        [self registerClass:UITableViewCell.class forCellReuseIdentifier:NSStringFromClass(UITableViewCell.class)];
        self.tableCanScroll = NO;
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(scrollNotification:) name:@"tableCanScroll" object:nil];
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(tableHeaderRefresh:) name:@"tableHeaderRefresh" object:nil];
        self.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
            [self.mj_footer beginRefreshing];
            //耗时0.5s模拟获取网络数据
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                [self addData:self.dataSources.count];
                [self reloadData];
                [self.mj_footer endRefreshing];
            });
            
        }];
        self.dataSources = [[NSMutableArray alloc] init];
        [self addData:0];
    }
    return self;
}

- (void)addData:(NSInteger)count
{
    for (NSInteger i = count; i < count + 20; i++) {
        [self.dataSources addObject:[NSString stringWithFormat:@"table-%ld -- cell-%ld", self.index, i]];
    }
}

- (void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self name:@"tableCanScroll" object:nil];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:@"tableHeaderRefresh" object:nil];
}

#pragma mark - UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataSources.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass(UITableViewCell.class) forIndexPath:indexPath];
    cell.textLabel.text = self.dataSources[indexPath.row];
    return cell;
}

#pragma mark - UITableViewDelegate
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 50;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
}

#pragma mark - UIScrollViewDelegate
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    NSLog(@"table -- %.2f", scrollView.contentOffset.y);
    
    if (!self.tableCanScroll) {
        [scrollView setContentOffset:CGPointZero];
    }
    
    if (scrollView.contentOffset.y <= 0) {
        _tableCanScroll = NO;
        NSLog(@"-*- 通知 scrollView 可滑动");
        [[NSNotificationCenter defaultCenter] postNotificationName:@"scrollCanScroll" object:nil userInfo:@{@"canScroll":@"1"}];
    }
    
}

#pragma mark - NSNotificationCenter
- (void)scrollNotification:(NSNotification *)notification
{
    NSDictionary *userInfo = notification.userInfo;
    NSString *canScroll = userInfo[@"canScroll"];
    if (canScroll.boolValue) {
        _tableCanScroll = YES;
    }
}

- (void)tableHeaderRefresh:(NSNotification *)notification
{
    //耗时0.5s模拟获取网络数据
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [self.dataSources removeAllObjects];
        [self addData:0];
        [self reloadData];
        NSLog(@"-*- 通知 scroll 停止刷新");
        [[NSNotificationCenter defaultCenter] postNotificationName:@"scrollHeaderRefreshStop" object:nil userInfo:nil];
    });
}

@end
